<script setup lang="ts">
import type { ArticleItem, ArticleResData } from '@/types/data'
import axios from 'axios'
import { ref, watch } from 'vue'

const props = defineProps<{
  channelIndex: number
}>()

const list = ref<ArticleItem[]>([])

watch(
  () => props.channelIndex,
  async () => {
    // console.log(props.channelIndex)
    const res = await axios.get<ArticleResData>('http://geek.itheima.net/v1_0/articles', {
      params: {
        // channel_id 频道 id
        // timestamp 时间戳 Date.now()
        channel_id: props.channelIndex,
        timestamp: Date.now()
      }
    })
    // console.log(res)
    list.value = res.data.data.results
  },
  {
    immediate: true
  }
)
</script>

<template>
  <div class="article-list">
    <div class="article-item" v-for="item in list" :key="item.art_id">
      <p class="title">{{ item.title }}</p>
      <img
        v-if="item.cover.type !== 0"
        v-for="(it, index) in item.cover.images"
        :key="index"
        class="img"
        :src="it"
        alt=""
      />
      <div class="info">
        <span>{{ item.aut_name }}</span>
        <span>{{ item.comm_count }}评论</span>
        <span>{{ item.pubdate }}</span>
      </div>
    </div>
  </div>
</template>
